package xyz.erupt.excel.util;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;

import java.text.SimpleDateFormat;
import java.util.Date;

import static org.apache.poi.ss.usermodel.CellType.NUMERIC;
import static org.apache.poi.ss.usermodel.DateUtil.getJavaDate;

public class DateFormatUtil {
    public static boolean isCellDateFormatted(Cell cell) {
        if (cell == null) {
            return false;
        }
        boolean bDate = false;

        double d = cell.getNumericCellValue();
        if (isValidExcelDate(d)) {
            CellStyle style = cell.getCellStyle();
            if (style == null) {
                return false;
            }
            int i = style.getDataFormat();
            String f = style.getDataFormatString();
            bDate = isADateFormat(i, f);
        }
        return bDate;
    }

    public static boolean isADateFormat(int formatIndex, String formatString) {
        if (isInternalDateFormat(formatIndex)) {
            return true;
        }

        if ((formatString == null) || (formatString.length() == 0)) {
            return false;
        }

        String fs = formatString;
        //下面这一行是自己手动添加的 以支持汉字格式wingzing
        fs = fs.replaceAll("[\"|\']", "").replaceAll("[年|月|日|时|分|秒|毫秒|微秒]", "");
        fs = fs.replaceAll("\\\\-", "-");
        fs = fs.replaceAll("\\\\,", ",");
        fs = fs.replaceAll("\\\\.", ".");
        fs = fs.replaceAll("\\\\ ", " ");
        fs = fs.replaceAll(";@", "");
        fs = fs.replaceAll("^\\[\\$\\-.*?\\]", "");
        fs = fs.replaceAll("^\\[[a-zA-Z]+\\]", "");
        return (fs.matches("^[yYmMdDhHsS\\-/,. :]+[ampAMP/]*$"));
    }

    public static boolean isInternalDateFormat(int format) {
        switch (format) {
            case 14:
            case 15:
            case 16:
            case 17:
            case 18:
            case 19:
            case 20:
            case 21:
            case 22:
            case 45:
            case 46:
            case 47:
            case 57:
            case 58:
                return true;
            case 23:
            case 24:
            case 25:
            case 26:
            case 27:
            case 28:
            case 29:
            case 30:
            case 31:
            case 32:
            case 33:
            case 34:
            case 35:
            case 36:
            case 37:
            case 38:
            case 39:
            case 40:
            case 41:
            case 42:
            case 43:
            case 44:
        }
        return false;
    }

    public static boolean isValidExcelDate(double value) {
        return (value > -4.940656458412465E-324D);
    }

    public static String toDateStr(Cell cell) {
        CellStyle cellStyle = cell.getCellStyle();
        short format = cellStyle.getDataFormat();
        SimpleDateFormat sdf;
        if (format == 20 || format == 32) {
            sdf = new SimpleDateFormat("HH:mm");
        } else if (format == 14 || format == 31 || format == 57 || format == 58) {
            sdf = new SimpleDateFormat("yyyy-MM-dd");
        } else {
            /**
             * 上面处理的时间，单元格格式都是自定义格式中时间格式，而下面解决的是单元格格式为日期格式的问题
             * 单元格格式为日期格式时，没办法通过format进行判断，所以进行如下操作：
             * 1、现将数据转换成 "yyyy-MM-dd HH:mm:ss"的格式
             * 2、将SimpleDateFormat类型的时间转换成String类型，并去除其中的空格、：和-三个符号，存进String类型数组中
             * 3、通过数组进行中时分秒的数据进行判断最终要展示的时间样式
             * 判断小时为0时，则不显示时分秒；判断秒钟为0时，则不显示年月日
             */
            sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String[] str = sdf.format(getJavaDate(cell.getNumericCellValue())).toString().split("-|:| ");
            if (Integer.parseInt(str[3]) == 0 && Integer.parseInt(str[4]) == 0 && Integer.parseInt(str[5]) == 0) {
                //根据表格，当时分秒都为0时，说明此单元格为生产日期，此处只显示年月日
                sdf = new SimpleDateFormat("yyyy-MM-dd");
            } else if (Integer.parseInt(str[3]) != 0 && Integer.parseInt(str[4]) != 0 && Integer.parseInt(str[5]) == 0) {
                //根据表格，当小时和分钟不为0的情况，说明此单元格为初凝和终凝，年月日是不需要的
                sdf = new SimpleDateFormat("HH:mm");
            } else {
                return String.valueOf(cell.getNumericCellValue());
            }
        }
        Date date = getJavaDate(cell.getNumericCellValue());
        return sdf.format(date);
    }
}
